package yxy.game.pm2.command.fortdefeat;

import yxy.cherry.data.script.fomula;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.Hero;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.FortDefeatMessage;
import yxy.game.pm2.message.HeroMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.fortdefeat.FortDefeatModule;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/** 副本过关斩将 过关斩将复活武将 */
@MessageType(proto = Command.FortDefeatRevive, classtype = FortDefeatMessage.FortDefeatReviveRequest.class)
public class CMD_FortDefeatRevive extends Command<FortDefeatMessage.FortDefeatReviveRequest> {

	@Override
	public void execute(MessageSource messageSource, FortDefeatMessage.FortDefeatReviveRequest request) {
		String playerCode = messageSource.getTag();
		List<String> codes = request.getCodesList();

		FortDefeatMessage.FortDefeatReviveResponse.Builder response = FortDefeatMessage.FortDefeatReviveResponse.newBuilder();

		// TODO 处理响应封装
		FortDefeatModule fortDefeatModule = FortDefeatModule.get(playerCode);
		try {
			if (fortDefeatModule.tryLock()) {
				Map<String, Integer> hps = fortDefeatModule.getHeroHp();
				for (String code : codes) {
					if (!hps.containsKey(code) || hps.get(code) > 0) {
						send(messageSource, response, Result.ParamError);// TODO 无需复活
						return;
					}
				}
				int costnum = 0;
				for (int i = 0; i < codes.size(); i++) {
					costnum += fomula.过关斩将复活花费元宝数.intValue(fortDefeatModule.getReviveTimes() + i);
				}
				Currency cost = Currency.create(CurrencyType.CT_元宝, 0, costnum);
				try {
					new CurrencyHandle().cost(playerCode, CCReason.过关斩将, cost);
				} catch (CurrencyNotEnoughException e) {
					send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));// 消费不足
					return;
				}
				for (String code : codes) {
					fortDefeatModule.getHeroHp().remove(code);
				}
				fortDefeatModule.setReviveTimes(fortDefeatModule.getReviveTimes() + codes.size());
				fortDefeatModule.set();

				// 血量信息
				for (Entry<String, Integer> entry : fortDefeatModule.getHeroHp().entrySet()) {
					String code = entry.getKey();
					int hp = entry.getValue();
					if (Hero.get(playerCode, code) != null) {
						HeroMessage.HeroHp.Builder builder = HeroMessage.HeroHp.newBuilder();
						builder.setCode(code);
						builder.setHp(hp);
						response.addHps(builder);
					}
				}
				response.setRevived(fortDefeatModule.getReviveTimes());

				send(messageSource, response, Result.Success);
			}
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		} finally {
			fortDefeatModule.unlock();
		}
	}

	private void send(MessageSource messageSource, FortDefeatMessage.FortDefeatReviveResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}